/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Tank.java]
 * Creation Date        [22-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_game.tankwar_local.version_3;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

/**
 * 坦克
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Tank {

	/** 坦克初始化坐标 */
	private int x = MainFrame.GAME_WIDTH / 2;
	private int y = MainFrame.GAME_HEIGHT / 2;

	/** 坦克移动速度 */
	public static final int X_SPEED = 5;
	public static final int Y_SPEED = 5;

	/** 坦克的宽度和高度 */
	public static final int TANK_WIDTH = 30;
	public static final int TANK_HEIGHT = 30;

	/** 标识方向键是否被按下 */
	private boolean l = false;
	private boolean r = false;
	private boolean u = false;
	private boolean d = false;

	/** 坦克默认的方向 */
	private Direction direction = Direction.STOP;

	/** 炮筒方向 */
	private Direction gunDirection = Direction.UP;

	/** 负责初始化MainFrame中的Missile */
	private MainFrame client;

	/** 判断敌人坦克 */
	private boolean alliance;

	/** 判断坦克是否消亡 */
	private boolean live = true;

	public Tank() {
		super();
	}

	public Tank(int x, int y, boolean alliance, MainFrame client) {
		super();
		this.x = x;
		this.y = y;
		this.alliance = alliance;
		this.client = client;
	}

	/**
	 * 绘制坦克
	 * 
	 * @param g
	 */
	public void paint(Graphics g) {
		if (!isLive()) {
			if (!isAlliance()) {
				client.enemyTanks.remove(this);
			}
			return;
		}
		// 获得原位置颜色
		Color color = g.getColor();
		// 设置敌我坦克颜色
		if (isAlliance()) {
			g.setColor(Color.GREEN);
		} else {
			g.setColor(Color.BLUE);
		}
		g.fillRect(x, y, TANK_WIDTH, TANK_HEIGHT);
		// 将前景色还原
		g.setColor(color);

		// 绘制炮筒
		switch (gunDirection) {
		case LEFT:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x - TANK_WIDTH
					/ 2, y + TANK_WIDTH / 2);
			break;
		case RIGHT:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH
					+ TANK_WIDTH / 2, y + TANK_HEIGHT / 2);
			break;
		case UP:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH
					/ 2, y - TANK_HEIGHT / 2);
			break;
		case DOWN:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH
					/ 2, y + TANK_HEIGHT + TANK_HEIGHT / 2);
			break;
		default:
			break;
		}

		// 坦克移动
		move();
	}

	/**
	 * 处理键盘按下事件
	 * 
	 * @param e
	 */
	public void keyPressed(KeyEvent e) {
		// 获得按键信息
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_A:
			fire();
			break;
		case KeyEvent.VK_S:
			superFire();
			break;
		case KeyEvent.VK_UP:
			u = true;
			break;
		case KeyEvent.VK_DOWN:
			d = true;
			break;
		case KeyEvent.VK_LEFT:
			l = true;
			break;
		case KeyEvent.VK_RIGHT:
			r = true;
			break;
		}

		setDirection();
	}

	/**
	 * 处理键盘释放事件
	 * 
	 * @param e
	 */
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_UP:
			u = false;
			break;
		case KeyEvent.VK_DOWN:
			d = false;
			break;
		case KeyEvent.VK_LEFT:
			l = false;
			break;
		case KeyEvent.VK_RIGHT:
			r = false;
			break;
		}
		setDirection();
	}

	/**
	 * 设定坦克方向
	 */
	private void setDirection() {
		if (l && !r && !u && !d) {
			direction = Direction.LEFT;
		} else if (!l && r && !u && !d) {
			direction = Direction.RIGHT;
		} else if (!l && !r && u && !d) {
			direction = Direction.UP;
		} else if (!l && !r && !u && d) {
			direction = Direction.DOWN;
		} else if (!l && !r && !u && !d) {
			direction = Direction.STOP;
		}
	}

	/**
	 * 坦克移动
	 */
	private void move() {
		switch (direction) {
		case LEFT:
			x -= X_SPEED;
			break;
		case RIGHT:
			x += X_SPEED;
			break;
		case UP:
			y -= Y_SPEED;
			break;
		case DOWN:
			y += Y_SPEED;
			break;
		case STOP:
			break;
		}
		// 炮筒的方向和坦克一致
		if (direction != Direction.STOP) {
			gunDirection = direction;
		}

		// 解决坦克出界问题
		if (x < 0) {
			x = 0;
		}
		if (y < 30) {
			y = 30;
		}
		if (x + TANK_WIDTH > MainFrame.GAME_WIDTH) {
			x = MainFrame.GAME_WIDTH - TANK_WIDTH;
		}
		if (y + TANK_HEIGHT > MainFrame.GAME_HEIGHT) {
			y = MainFrame.GAME_HEIGHT - TANK_HEIGHT;
		}
	}

	/** 坦克开火, 根据坦克的坐标和方向来生成导弹 */
	public Missile fire() {
		// 开火位置位于坦克的中心
		int x = this.x + Tank.TANK_WIDTH / 2 - Missile.MISSILE_WIDTH / 2;
		int y = this.y + Tank.TANK_HEIGHT / 2 - Missile.MISSILE_HEIGHT / 2;
		Missile missile = new Missile(x, y, gunDirection, client);
		client.missiles.add(missile);
		return missile;
	}

	/** 碰撞检测 */
	public Rectangle getRectangle() {
		return new Rectangle(x, y, TANK_WIDTH, TANK_HEIGHT);
	}

	public boolean isAlliance() {
		return alliance;
	}

	public void setAlliance(boolean alliance) {
		this.alliance = alliance;
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Tank other = (Tank) obj;

		// 防止生成位置和我方坦克位置重合
		Rectangle rectangle = new Rectangle(MainFrame.GAME_WIDTH / 2,
				MainFrame.GAME_HEIGHT / 2, TANK_WIDTH, TANK_HEIGHT);

		// 根据是否相交判断坦克生成位置是否相同
		if (getRectangle().intersects(
				new Rectangle(other.x, other.y, TANK_WIDTH, TANK_HEIGHT))
				&& getRectangle().intersects(rectangle)) {
			return true;
		}
		return false;
	}

	/**
	 * 超级火力
	 */
	private void superFire() {
	}
}
